package com.waimai.controllor.ele;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.htmlparser.Node;
import org.htmlparser.filters.HasAttributeFilter;
import org.htmlparser.util.NodeList;

import com.waimai.adapter.RestaurantAdapter;
import com.waimai.constant.URLConstant;
import com.waimai.controllor.BaseControllor;
import com.waimai.controllor.HttpControllor;
import com.waimai.controllor.Interface.IHtmlControllor;
import com.waimai.controllor.Interface.IHttpControllor;
import com.waimai.entity.Address;
import com.waimai.entity.Restaurant;
import com.waimai.entity.Sale;
import com.waimai.log.Log;
import com.waimai.utils.HtmlParserUtils;
import com.waimai.utils.htmlStrUtils;

public class EleHttpControllor extends BaseControllor implements
		IHttpControllor {

	private static EleHttpControllor controllor;

	public static EleHttpControllor getInstance() {
		if (controllor == null) {
			controllor = new EleHttpControllor();
		}
		return controllor;
	}

	@Override
	public Map<String, RestaurantAdapter> getRestaurants(Address... addresses) {
		URI uri = null;
		Map<String, RestaurantAdapter> restaurantMap = new HashMap<String, RestaurantAdapter>();
		for (Address address : addresses) {
			try {
				uri = new URI(URLConstant.URL_ELE + address.getEleHref());
			} catch (URISyntaxException e) {
				Log.e(e, "ele uri error");
				e.printStackTrace();
			}
			HttpControllor controllor = HttpControllor.getInstance();
			restaurantMap.putAll(getRestaurantsByBytes(
					controllor.getHtmlBytes(uri), address));
		}
		return restaurantMap;
	}

	/**
	 * 
	 * 
	 * @param bytes
	 * @return
	 * 
	 */
	private Map<String, RestaurantAdapter> getRestaurantsByBytes(byte[] bytes,
			Address address) {
		IHtmlControllor htmlControllor = EleHtmlControllor.getInstance();
		RestaurantAdapter r = new RestaurantAdapter();
		before();
		Map<String, RestaurantAdapter> restaurantMap = new HashMap<String, RestaurantAdapter>();
		if (bytes != null) {
			// 按键值对查
			// 减钱商家
			NodeList nodeList = HtmlParserUtils.getNodeListByBytes(bytes,
					URLConstant.CHARSET, new HasAttributeFilter("class",
							"restaurant-icons extra-discount"));
			Log.i("eleRestaurantsNum = " + nodeList.size());
			// System.out.println("ele" + nodeList.size());
			if (nodeList.size() != 0) {
				for (int i = 0; i < nodeList.size(); i++) {
					Node minusNode = nodeList.elementAt(i);
					Node parentNode = minusNode;
					ArrayList<Sale> saleList = new ArrayList<Sale>();
					try {

						String saleString = parentNode.getText();
						saleList = htmlStrUtils.getEleSaleList(saleString);

						// 获得父节点
						while (true) {
							// 节点有两种，一种有 closed 一种没有，取一样的
							if (parentNode.getText().contains(
									"restaurant-block")) {
								break;
							}
							parentNode = parentNode.getParent();
						}

						// href
						String hrefUrl = htmlControllor.getHrefUrl(parentNode);
						// 图标
						String imgUrl = htmlControllor.getImgUrl(parentNode);

						String name = htmlControllor.getName(parentNode);
						String startPrice = htmlControllor
								.getStartPrice(parentNode);
						// 送餐时间
						String sendTime = htmlControllor
								.getSendTime(parentNode);
						String sendPrice = htmlControllor
								.getSendPrice(parentNode);
						String totalPerMon = htmlControllor
								.getTotalPerMon(parentNode);
						int maxBack = htmlControllor.getMaxBack(parentNode);
						// 休息
						int status = htmlControllor.getStatus(parentNode);

						// 饮料
						ArrayList<String> drinkList = htmlControllor
								.getDrink(parentNode);

						// 公司
						int company = htmlControllor.getCompany();

						String id = company + hrefUrl;

						r = new RestaurantAdapter(new Restaurant(id, name,
								saleList, hrefUrl, drinkList, company, maxBack,
								status, imgUrl, totalPerMon), address,
								sendTime, startPrice, sendPrice);
					} catch (Exception e) {
						// TODO: handle exception
						Log.e(e, "getEleRestaurants error");
						e.printStackTrace();
					}
					restaurantMap.put(r.getId(), r);
				}
			} else {
				Log.e("bytes is null");
			}
		}
		after("getEleRestaurants");
		return restaurantMap;

	}

}
